<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/praise_functions.php';

session_start();
require_once 'login_check.php';
require_once 'manage_index.php';
check_admin_session();

// 处理表单提交（新增、批量展示、批量停止展示、批量删除）
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    // 新增
    if ($action === 'add') {
        $stmt = $pdo->prepare("INSERT INTO announcements (title, content, type, display_order, is_active, updated_at) VALUES (?, ?, ?, ?, ?, NOW())");
        $stmt->execute([
            $_POST['title'],
            $_POST['content'],
            $_POST['type'],
            intval($_POST['display_order']),
            isset($_POST['is_active']) ? 1 : 0
        ]);
    }
    // 批量展示
    elseif ($action === 'batch_show' && !empty($_POST['edit_ids'])) {
        foreach ($_POST['edit_ids'] as $id) {
            $stmt = $pdo->prepare("UPDATE announcements SET is_active=1, updated_at=NOW() WHERE id=?");
            $stmt->execute([intval($id)]);
        }
    }
    // 批量停止展示
    elseif ($action === 'batch_hide' && !empty($_POST['edit_ids'])) {
        foreach ($_POST['edit_ids'] as $id) {
            $stmt = $pdo->prepare("UPDATE announcements SET is_active=0, updated_at=NOW() WHERE id=?");
            $stmt->execute([intval($id)]);
        }
    }
    // 批量删除
    elseif ($action === 'batch_delete' && !empty($_POST['edit_ids'])) {
        $ids = array_map('intval', $_POST['edit_ids']);
        $in = implode(',', $ids);
        $pdo->exec("DELETE FROM announcements WHERE id IN ($in)");
    }
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}

function get_all_announcements()
{
    global $pdo;
    $stmt = $pdo->query("SELECT * FROM announcements ORDER BY type, display_order ASC, updated_at DESC");
    $result = $stmt ? $stmt->fetchAll() : [];
    return $result ?: [];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公告管理</title>
    <!-- Google Fonts for Modern Look -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
    <!-- Modern Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@7.4.47/css/materialdesignicons.min.css">
    <style>
    body {
        font-family: 'Inter', 'Segoe UI', 'Helvetica Neue', Arial, 'PingFang SC', 'Microsoft YaHei', sans-serif;
        background: #f5f7fa;
        margin: 0;
        display: flex;
        min-height: 100vh;
    }


    .main-content {
        margin-left: 240px;
        width: 100%;
        padding: 48px 32px 32px 32px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .card {
        background: #fff;
        border-radius: 16px;
        box-shadow: 0 4px 18px rgba(41,128,255,0.08);
        margin-bottom: 36px;
        width: 100%;
        max-width: 1000px;
        padding: 32px;
        display: flex;
        flex-direction: column;
        align-items: stretch;
        transition: box-shadow 0.2s;
    }
    .card:hover {
        box-shadow: 0 8px 24px rgba(41,128,255,0.12);
    }
    .card-title {
        font-size: 1.45em;
        font-weight: bold;
        color: #000000;
        margin-bottom: 18px;
        letter-spacing: 1.5px;
        display: flex;
        align-items: center;
    }
    .card-title .mdi {
        font-size: 1.5em;
        margin-right: 12px;
    }
    .table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 12px;
        table-layout: fixed;
        font-size: 1em;
        background: transparent;
    }
    .table th, .table td {
        padding: 12px 14px;
        border-bottom: 1px solid #eaeaea;
        text-align: left;
        word-break: break-all;
        background: transparent;
    }
    .table th {
        background: #f0f5ff;
        color: #2980ff;
        font-weight: 700;
        letter-spacing: 1px;
        border-bottom: 2px solid #2980ff;
    }
    .table tr:last-child td {
        border-bottom: none;
    }
    .table tr.inactive {
        color: #aaa;
    }
    .no-data {
        text-align: center;
        color: #888;
        font-size: 1.05em;
        padding: 18px 0;
    }
    .form-group {
        display: flex;
        flex-wrap: wrap;
        gap: 16px;
        margin-bottom: 24px;
        align-items: center;
    }
    .form-group label {
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .form-control {
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-family: inherit;
        font-size: 1em;
        width: 180px;
    }
    .form-control:focus {
        outline: none;
        border-color: #2980ff;
        box-shadow: 0 0 0 2px rgba(41, 128, 255, 0.2);
    }
    .form-control[type="number"] {
        width: 100px;
    }
    .btn {
        padding: 8px 16px;
        background: #2980ff;
        color: #fff;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        font-family: inherit;
        font-size: 1em;
        transition: background 0.2s, box-shadow 0.2s;
    }
    .btn:hover {
        background: #1866d1;
        box-shadow: 0 2px 8px rgba(41, 128, 255, 0.3);
    }
    .btn.show {
        background: #43b362;
    }
    .btn.show:hover {
        background: #359246;
    }
    .btn.hide {
        background: #2980ff;
    }
    .btn.hide:hover {
        background: #1866d1;
    }
    .btn.del {
        background: #ff4d4f;
    }
    .btn.del:hover {
        background: #d9363e;
    }
    .actions {
        display: flex;
        gap: 12px;
        margin-top: 16px;
    }
    .link-group {
        margin-top: 24px;
        align-self: flex-start;
    }
    .link-group a {
        color: #2980ff;
        text-decoration: none;
        margin-right: 16px;
        transition: color 0.2s;
    }
    .link-group a:hover {
        color: #1866d1;
        text-decoration: underline;
    }
    @media (max-width: 900px) {
        .main-content { margin-left: 56px; padding: 24px 8px; }
        .card { max-width: 99vw; padding: 16px;}
        .form-group { flex-direction: column; align-items: flex-start; gap: 12px; }
        .form-control { width: 100%; }
        .actions { flex-wrap: wrap; }
        .btn { flex: 1; min-width: 120px; }
    }
    @media (max-width: 600px) {
        .main-content { padding: 8px 2px;}
        .card { padding: 12px;}
        .table th, .table td { padding: 8px 6px; font-size: 0.9em; }
    }
    </style>
</head>
<body>
    <?php render_sidebar(); ?>
    <div class="main-content">
        <div class="card">
             
            <div class="card-title"><span class="mdi mdi-bullhorn"></span>公告管理</div>
            <!-- 添加公告表单 -->
            <form method="post" style="margin-bottom:24px;">
                <input type="hidden" name="action" value="add">
                <div class="form-group">
                    <label>标题: <input type="text" name="title" class="form-control" required></label>
                    <label>内容: <input type="text" name="content" class="form-control" required style="width: 250px;"></label>
                    <label>类型:
                        <select name="type" class="form-control">
                            <option value="notice">通知</option>
                            <option value="important">重要</option>
                        </select>
                    </label>
                    <label>排序: <input type="number" name="display_order" class="form-control" value="0"></label>
                    <label>展示: <input type="checkbox" name="is_active" checked></label>
                    <button class="btn" type="submit">添加公告</button>
                </div>
            </form>
            
            <!-- 公告列表 -->
            <form method="post">
                <table class="table">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="allCheck" onclick="toggleAll(this)"></th>
                            <th>ID</th>
                            <th>标题</th>
                            <th>内容</th>
                            <th>类型</th>
                            <th>排序</th>
                            <th>展示</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $all = get_all_announcements();
                        foreach ($all as $row): ?>
                            <tr<?= !$row['is_active'] ? ' class="inactive"' : '' ?>>
                                <td>
                                    <input type="checkbox" name="edit_ids[]" value="<?= $row['id'] ?>">
                                </td>
                                <td><?= $row['id'] ?></td>
                                <td><?= htmlspecialchars($row['title']) ?></td>
                                <td><?= htmlspecialchars($row['content']) ?></td>
                                <td><?= $row['type'] == 'notice' ? '通知' : '重要' ?></td>
                                <td><?= $row['display_order'] ?></td>
                                <td><?= $row['is_active'] ? '√' : '×' ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($all)): ?>
                            <tr><td colspan="7" class="no-data">暂无公告数据</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
                
                <div class="actions">
                    
                    <button class="btn show" type="submit" name="action" value="batch_show"
                        onclick="return confirm('确定批量展示所选公告？')">批量展示</button>
                    <button class="btn hide" type="submit" name="action" value="batch_hide"
                        onclick="return confirm('确定批量停止展示所选公告？')">批量停止展示</button>
                    <button class="btn del" type="submit" name="action" value="batch_delete"
                        onclick="return confirm('确定批量删除所选公告？操作不可恢复！')">批量删除</button>
                </div>
            </form>
            
        </div>
    </div>

    <script>
        function toggleAll(src) {
            var cbs = document.querySelectorAll('input[name="edit_ids[]"]');
            for (let cb of cbs) cb.checked = src.checked;
        }
    </script>
</body>
</html>